/********************************************
 * 功能说明: Web端Authority基础业务控制器
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.controller;

import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.Lists;
import com.querydsl.core.types.Predicate;
import com.spring.arch.common.dto.result.ListResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.dto.AuthorityDTO;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.converter.AuthorityConverter;
import com.spring.arch.common.dto.result.ListResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.dto.AuthorityDTO;
import com.spring.arch.uaa.model.Authority;
import com.spring.arch.uaa.repository.AuthorityRepository;
import com.spring.arch.uaa.service.AuthorityService;
import com.spring.arch.uaa.web.converter.AuthorityConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001005001;


/**
 * Web端Authority基础业务控制器
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Slf4j
@RestController
@RequestMapping(value = "/uaa/w/authority", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = {"Web端Authority管理API" })
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private AuthorityRepository authorityRepository;
    @Autowired
    private AuthorityConverter authorityConverter;

    /**
     * 新建操作
     *
     * @param authorityDTO 新建资源的DTO
     * @return 新建资源
     */
    @Timed
    @ApiOperation(value = "新建操作")
    @PostMapping
    public ResultDTO<AuthorityDTO> create(@RequestBody @Valid final AuthorityDTO authorityDTO) {
        final Authority model = this.authorityConverter.toModel(authorityDTO);
        this.authorityService.save(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Authority.class.getSimpleName(), model.getId());
        }
        return this.authorityConverter.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param id 实体对象ID
     * @return 操作结果
     */
    @Timed
    @ApiOperation(value = "删除操作")
    @DeleteMapping(value = "/{id}")
    public ResultDTO<Void> delete(@PathVariable final UUID id) {
        this.authorityService.deleteById(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was disable.", Authority.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    /**
     * 更新操作
     *
     * @param id 实体对象ID
     * @param authorityDTO 更新资源的DTO
     * @return 更新后资源
     */
    @Timed
    @ApiOperation(value = "更新操作")
    @PutMapping(value = "/{id}")
    public ResultDTO<AuthorityDTO> update(@PathVariable final UUID id, @RequestBody @Valid final AuthorityDTO authorityDTO) {
        authorityDTO.setId(id);
        final Authority model = this.authorityConverter.toModel(authorityDTO);
        this.authorityService.save(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Authority.class.getSimpleName(), model.getId());
        }
        return this.authorityConverter.toResultDTO(model);
    }

    /**
     * 获取分页数据
     *
     * @return 分页数据
     */
    @Timed
    @ApiOperation(value = "获取分页数据")
    @PostMapping(value = "/s")
    public ListResultDTO<AuthorityDTO> search(@QuerydslPredicate(root = Authority.class) Predicate predicate) {
        Iterable<Authority> authorities = this.authorityRepository.findAll(predicate);
        return ListResultDTO.success(authorityConverter.toTree(Lists.newArrayList(authorities)));
    }

    /**
     * 取得详细数据
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "获取详细数据")
    @GetMapping(value = "/{id}")
    public ResultDTO<AuthorityDTO> get(@PathVariable final UUID id) {
        final Authority model = this.authorityRepository.findById(id)
                    .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001005001, Authority.class.getSimpleName()));
        return this.authorityConverter.toResultDTO(model);
    }
}
